package com.bjc.core.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.bjc.base.core.annotation.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.PermissionTreeVO;
import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.core.annotation.PermissionPath;
import com.bjc.core.bean.request.RolePermissionReqVO;
import com.bjc.core.service.IPermissionService;
import com.bjc.core.service.IRolePermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 角色权限控制器
 * 
 * @author 许自辉
 * @date 2017年8月2日
 */
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/角色权限配置")
@Api(value = "role-api", tags = { "角色相关接口" })
public class RolePermissionController {

	@Autowired
	private IPermissionService permissionService;
	@Autowired
	private IRolePermissionService rolePermissionService;

	@ApiOperation("查询角色的权限树")
	@Permission("rolePermission:findTreeHave")
	@GetMapping("rolePermission/findTreeHave")
	public ResultVO findTreeHave(@Validated RolePermissionReqVO rolepermissionReqVO) {
		List<PermissionTreeVO> treelist = rolePermissionService.getTree(rolepermissionReqVO);
		treelist = PermissionTreeVO.getTreeList(treelist);
		PermissionTreeVO.removeNullTree(treelist);
		return ResultGenerator.getSuccess(treelist);
	}

	@ApiOperation("批量添加角色的权限")
	@Permission("rolePermission:createList")
	@PostMapping("rolePermission/createList")
	public ResultVO createListRolePermissions(@RequestBody RolePermissionReqVO rpvo) {
		List<RolePermissionReqVO> reqs = new ArrayList<RolePermissionReqVO>();
		int roleId = rpvo.getRoleId();
		Integer[] permissionIds = rpvo.getPermissionIds();
		if (roleId != 0 && permissionIds != null && permissionIds.length > 0) {
			Arrays.asList(permissionIds).stream().forEach(s -> {
				RolePermissionReqVO req = new RolePermissionReqVO();
				req.setRoleId(roleId);
				req.setPermissionId(s);
				reqs.add(req);
			});
		}
		int count = rolePermissionService.insertList(reqs);
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("批量删除角色权限")
	@Permission("rolePermission:deleteList")
	@DeleteMapping("rolePermission/deleteList")
	public ResultVO deleteRolePermissionById(
			@ApiParam(value = "角色权限id集合", required = true) @RequestBody int[] ids) {
		int count = rolePermissionService.deleteList(ids);
		return ResultGenerator.getDeleteSuccess(count);
	}

	@ApiOperation("删除角色无用权限")
	@Permission("rolePermission:delete:destroy")
	@DeleteMapping("rolePermission/delete/destroy")
	public ResultVO deleteRolePermissionById() {
		rolePermissionService.deleteDestroy();
		permissionService.deleteDestroy();
		return ResultGenerator.getSuccess();
	}

}
